<?php
/**
 * @since      File available since Release v1.1
 */

class shopControl extends BaseHomeControl{
	public function __construct(){
		parent::__construct();
        Language::read('indexs');
        Tpl::output('index_sign','index');
        
        
	}
	
	public function header(){
		$model = Model();
		$seller_id = intval($_GET['seller_id']);
		$seller = $model->table('user')->where(array('user_id'=>$seller_id))->find();
		if(empty($seller)) exit;
		
		
		$fcount = $model->table('friend')->where(array('fid'=>$seller['user_id']))->count();
		Tpl::output('fcount',$fcount);//粉丝数
		
		//seller pingfen
		$pingfen = array();
		$pingfen['order_state'] = '3';
		$pingfen['is_fail'] = '0';
		$pingfen['order_comment'] = array('neq','');
		$pingfen['seller_id'] = $seller_id;
		
		$pingfen_sum = $model->table('order')->where($pingfen)->sum('order_score');
		$pingfen_count = $model->table('order')->where($pingfen)->count();
		$seller_pingfen = $pingfen_count=='0'?'0.00':number_format(($pingfen_sum/$pingfen_count),2);
		Tpl::output('seller_pingfen',$seller_pingfen);
		
		
		if($_GET['seller_id']!=$_COOKIE['user_id']){
			//关注 未关注
			$condition = array();
			$condition['uid'] = $_COOKIE['user_id'];
			$condition['fid'] = $seller['user_id'];
			$guanzhu = $model->table('friend')->where($condition)->find();
			if(!empty($guanzhu)){
				Tpl::output('guanzhu','1');
			}else{
				Tpl::output('guanzhu','0');
			}
		}
		
		
		$level = array();
		$level['start'] = array('elt',$seller['spoints']);
		$level['end'] = array('gt',$seller['spoints']);
		$level['type'] = 1;
		$slevel = $model->table('level')->where($level)->find();
		$seller['slevel'] = $slevel['name'];
		
		Tpl::output('seller',$seller);
	}
	
	/*
	 * 店铺首页 
	 */
	public function indexOp(){
		$this->header();
			
		$condition = array();
		$condition['end_time'] = array('gt',time());
		$condition['is_fabu'] = 1;
		$condition['is_state'] = '0';
		$condition['is_show'] = '0';
		$condition['seller_id'] = intval($_GET['seller_id']);
		$condition['yikoujia_end'] = '0';
		
		$model = Model();
		$count = $model->table('auction')->where($condition)->count();
		
		Tpl::output('count',$count);
		Tpl::showpage('shop.index');
	}
	
	
	/*
	 * 新品开拍数据
	 */
	public function newsdataOp(){
		$model = Model();
		$pagesize = 4;
		$pagenum = isset($_GET['nextpage'])?intval($_GET['nextpage']):1;
		if($pagenum == 1){
			$limit = "0,{$pagesize}";
		}else{
			$pagenum = ($pagenum-1)*$pagesize;
			$limit = "{$pagenum},{$pagesize}";
		}
		
		
		$condition = array();
		$condition['end_time'] = array('gt',time());
		$condition['is_fabu'] = 1;
		$condition['is_state'] = '0';
		$condition['is_show'] = '0';
		$condition['seller_id'] = intval($_GET['seller_id']);
		$condition['yikoujia_end'] = '0';
		
		$auction = $model->table('auction')->where($condition)->order('id desc')->limit($limit)->select();
		if(empty($auction)) exit;
		
		Tpl::output('list',$auction);
		
		Tpl::showpage('shop.newsdata');
	}
	
	/*
	 * 即将截拍
	 */
	public function distancetimeOp(){		
		$this->header();	
		
		$model = Model();
		$condition = array();
		$condition['end_time'] = array('gt',time());
		$condition['is_fabu'] = 1;
		$condition['is_state'] = '0';
		$condition['is_show'] = '0';
		$condition['seller_id'] = intval($_GET['seller_id']);
		$condition['yikoujia_end'] = '0';
		
		$count = $model->table('auction')->where($condition)->count();
		Tpl::output('count',$count);
		Tpl::showpage('shop.distancetime');
	}
	
	/*
	 * 即将数据
	*/
	public function distancetimedataOp(){
		$model = Model();
		$pagesize = 4;
		$pagenum = isset($_GET['nextpage'])?intval($_GET['nextpage']):1;
		if($pagenum == 1){
			$limit = "0,{$pagesize}";
		}else{
			$pagenum = ($pagenum-1)*$pagesize;
			$limit = "{$pagenum},{$pagesize}";
		}
	
	
		$condition = array();
		$condition['end_time'] = array('gt',time());
		$condition['is_fabu'] = 1;
		$condition['is_state'] = '0';
		$condition['is_show'] = '0';
		$condition['seller_id'] = intval($_GET['seller_id']);
		$condition['yikoujia_end'] = '0';
		
		$auction = $model->table('auction')->where($condition)->order('end_time asc')->limit($limit)->select();
		if(empty($auction)) exit;
	
		Tpl::output('list',$auction);
	
		Tpl::showpage('shop.distancetimedata');
	}
	
	
	/*
	 * 参拍拍品
	 */
	public function bidauctionOp(){
		$this->header();
		
		$model = Model();
		$condition = array();
		$condition['auction.end_time'] = array('gt',time());
		$condition['auction.is_fabu'] = 1;
		$condition['auction.is_state'] = '0';
		$condition['auction.is_show'] = '0';
		$condition['auction.seller_id'] = intval($_GET['seller_id']);
		$condition['auction.yikoujia_end'] = '0';
		$condition['auction_log.user_id'] = $_COOKIE['user_id'];
		
		$field = 'auction.*';
		$count = $model->field('auction_log.auction_id')->distinct(true)->table('auction,auction_log')->join('left')->on('auction.id=auction_log.auction_id')->where($condition)->count();
		Tpl::output('count',$count);
		Tpl::showpage('shop.bidauction');
	}
	
	public function bidauctiondataOp(){
		
		$model = Model();
		$pagesize = 4;
		$pagenum = isset($_GET['nextpage'])?intval($_GET['nextpage']):1;
		if($pagenum == 1){
			$limit = "0,{$pagesize}";
		}else{
			$pagenum = ($pagenum-1)*$pagesize;
			$limit = "{$pagenum},{$pagesize}";
		}
		
		
		$condition = array();
		$condition['auction.end_time'] = array('gt',time());
		$condition['auction.is_fabu'] = 1;
		$condition['auction.is_state'] = '0';
		$condition['auction.is_show'] = '0';
		$condition['auction.seller_id'] = intval($_GET['seller_id']);
		$condition['auction.yikoujia_end'] = '0';
		$condition['auction_log.user_id'] = $_COOKIE['user_id'];
		
		$field = 'auction.*';	
		$auction = $model->field('auction_log.auction_id')->distinct(true)->table('auction,auction_log')->join('left')->on('auction.id=auction_log.auction_id')->where($condition)->order('auction.id desc')->limit($limit)->select();
		if(empty($auction)) exit;
		
		foreach($auction as $k=>$val){
			$a = $model->table('auction')->where(array('id'=>$val['auction_id']))->find();
			$auction[$k]['id'] = $a['id'];
			$auction[$k]['auction_add_date'] = $a['auction_add_date'];
			$auction[$k]['auction_img1'] = $a['auction_img1'];
			$auction[$k]['auction_description'] = $a['auction_description'];
			$auction[$k]['leadingprice'] = $a['leadingprice'];
			$auction[$k]['viewnum'] = $a['viewnum'];
		}
		
		Tpl::output('list',$auction);
		Tpl::showpage('shop.bidauctiondata');
	}
	
	
	/*
	 * 截拍拍品
	 */
	public function endauctionOp(){
		$this->header();
		
		$model = Model();
		
		$condition = array();
		$condition['auction_order_sn'] = array('neq','0');
		$condition['seller_id'] = intval($_GET['seller_id']);
		
		$count = $model->table('auction')->where($condition)->count();
		Tpl::output('count',$count);
		Tpl::showpage('shop.endauction');
	}
	
	/*
	 * 截拍拍品
	*/
	public function endauctiondataOp(){
		
		$model = Model();
		$pagesize = 4;
		$pagenum = isset($_GET['nextpage'])?intval($_GET['nextpage']):1;
		if($pagenum == 1){
			$limit = "0,{$pagesize}";
		}else{
			$pagenum = ($pagenum-1)*$pagesize;
			$limit = "{$pagenum},{$pagesize}";
		}
		
		$condition = array();
		$condition['auction_order_sn'] = array('neq','0');
		$condition['seller_id'] = intval($_GET['seller_id']);

		
		$auction = $model->table('auction')->where($condition)->order('id desc')->limit($limit)->select();
		if(empty($auction)) exit;
		
		Tpl::output('list',$auction);
		
		Tpl::showpage('shop.endauctiondata');
	}
	
	
	
	
}